﻿using UnityEngine;
using System.Collections;

public class ObjectlistLabel : MonoBehaviour {
	BUILD_TYPE enLastType;
	public BuildData m_buildData;

	void Start(){
		m_buildData = GameObject.Find("DataLogic").GetComponent<DataManager>().GetBuildData (GetComponent<BuildingID>().enType , GetComponent<BuildingID>().iIndex);
		GetComponent<UILabel> ().text = m_buildData.strName;

		GetComponent<UILabel> ().depth = transform.parent.parent.GetComponent<UISprite> ().depth + 1;
	}

	void Update () {

	}

	void LateUpdate(){
		enLastType = GetComponent<BuildingID> ().enType;
	}

	void OnClick(){
		transform.parent.parent.GetComponent<BuildingID>().iIndex = GetComponent<BuildingID>().iIndex;
        transform.parent.parent.Find("SelectedObj").GetComponent<UISprite>().enabled = true;
        transform.parent.parent.Find("SelectedObj").GetComponent<UISprite>().depth = GetComponent<UILabel>().depth;
        transform.parent.parent.Find("SelectedObj").localPosition = transform.localPosition;

        GameObject.Find("MainCamera").GetComponent<AudioSource>().PlayOneShot(Resources.Load<AudioClip>("Audio/Click"));
	}

    void OnHover(bool isOver)
    {
        if (isOver)
        {
            transform.parent.parent.Find("HoveredObj").GetComponent<UISprite>().enabled = true;
            transform.parent.parent.Find("HoveredObj").GetComponent<UISprite>().depth = GetComponent<UILabel>().depth;
            transform.parent.parent.Find("HoveredObj").localPosition = transform.localPosition;
        }
        else
        {
            transform.parent.parent.Find("HoveredObj").GetComponent<UISprite>().enabled = false;
        }
    }

	public void DestroyThis()
	{
		Destroy (gameObject);
	}
}
